-- UISubAreaList
-- Create by zouyb
-- 子区域列表

require "game/ui/form/main/UINewDungeonEffect"
require "game/ui/form/dungeon/UIEnterDungeon"
require "game/ui/form/combat_stat/UIMopUp"
require "game/ui/form/main/UIMopUpEffect"
require "game/ui/form/achievement/UIDungeonAchiv"
require "game/ui/form/ranklist/UIRankNick"

-- UISubAreaList继承自Layer
UISubAreaList = class("UISubAreaList", function()
    return cc.Layer:create();
end);

function UISubAreaList.create(areaId)
    return UISubAreaList.new(areaId);
end

-- 内部函数声明
local resize;

-- 构造函数
function UISubAreaList:ctor(areaId)
    self:setName("UISubAreaList");

    self.areaId = areaId;

    -- 这边初始化一下当前经验值，在扫荡时候会用到
    self.exp = ME.user:getExp();
    self.level = ME.user:getLevel();

    -- 初始化
    local node = cc.CSLoader:createNode("layout/main/SubMapList.csb");
    self:addChild(node);
    self.node = node;

    -- 设置标题样式
    local titleLabel = findChildByName(node, "TOP/title_bg/text");
    TextStyleM.setOutlineStyle(titleLabel, 1, TextStyleM.TEXT_OUTLINE_COLOR, true);
    titleLabel:setString(DungeonAreaM.query(self.areaId, "name"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 适配
    resize(self);

    self:init(true);

    -- 显示扫荡次数
    self:updateMopupTimes();

    -- 显示底部按钮
    self:redrawButtons();

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISubAreaList");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册迷宫事件
    local btnDungeon = findChildByName(self.node, "BT/btn_dungeon");
    local function onDungeonClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 显示迷宫成就界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonAchiv");
            local uiDungeonAchiv = UIDungeonAchiv.create(self.areaId);
            UIMgr.getCurrentScene():addForm(uiDungeonAchiv);
            AudioM.playFx("ui_open1");
        end
    end
    btnDungeon:addTouchEventListener(onDungeonClick);
    TextStyleM.setTextStyle(btnDungeon, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnDungeon:setTitleText(getLocStr("dungeon_event"));

    -- 排行榜按钮
    local lbBtn = findChildByName(node, "BT/leaderboard");
    local function onLbClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            -- 判断是否同意查看排行榜
            if TopM.agreeOpen() then


                -- 首次进入排行榜，必须输入昵称和国籍
                if TopM.isFirstVisitTop() then
                    self:showRankNickInput();
                else
                    -- 打开排行榜
                    Operation.cmd_get_top_list(self.areaId);
                    self:openRankingList();
                end
            else
                local function confirm()
                    -- Operation.cmd_ios_agree_open_toplist();
                    -- 本地记录一下即可，不通过服务器
                    UserDefaultM.setStringForKey("is_agree_open_top", "1");
                end

                -- 打开确认界面
                confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("join_rank_list"), getLocStr("join_confirm"),
                    confirm, nil, getLocStr("join"), getLocStr("do_not_join"));
            end
        end
    end
    lbBtn:addTouchEventListener(onLbClick);
    TextStyleM.setTextStyle(lbBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    lbBtn:setTitleText(getLocStr("leaderboard"));

    EventMgr.register("UISubAreaList", event.DUNGEON_SUMMARY, function(summary)
        self:updateLayer(summary);
    end);

    -- 关注扫荡成功的回调
    EventMgr.register("UISubAreaList", event.MOP_UP_DONE, function(args)
        -- 提示扫荡成功
        self:whenMopUpDone(args);
    end);

    -- 关注同意查看排行榜的事件
    EventMgr.register("UISubAreaList", event.AGREE_JOIN_TOP_LIST, function(args)
        -- 首次进入排行榜，必须输入昵称和国籍
        if TopM.isFirstVisitTop() then
            self:showRankNickInput();
        else
            -- 打开排行榜
            Operation.cmd_get_top_list(self.areaId);
            self:openRankingList();
        end
    end);

    -- 注册领取成就奖励的事件回调
    EventMgr.register("UISubAreaList", event.TAKE_ACHIV_BONUS, function(args)
        -- 重绘迷宫事件提醒
        self:redrawDungeonAchivBubble();

        self:init();

        -- 检查扫荡按钮是否可用
        self:updateMopupTimes();
    end);

    -- 注册获得焦点的回调
    EventMgr.register("UISubAreaList", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UISubAreaList" then
            local btNode = findChildByName(self.node, "BT");
            btNode:setVisible(true);

            if UIMgr:getCurrentScene().topMenu then
                UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_TREE, TOPMENU_FRAME_LIFE);
            end
        elseif para["lose"] == "UISubAreaList" then
            local btNode = findChildByName(self.node, "BT");
            btNode:setVisible(false);
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            EventMgr.removeAll("UISubAreaList");

            if UIMgr:getCurrentScene():isOpen("UIGainItemPath") then
                local uiForm = UIMgr:getCurrentScene():getFormByName("UIGainItemPath");
                uiForm:redraw();
            else
                if UIMgr.getCurrentScene().mainMap then
                    UIMgr.getCurrentScene().mainMap:resumeEffect();

                    if DungeonM.isInDungeon() then
                        if UIMgr.getCurrentScene().topMenu then
                            UIMgr.getCurrentScene().topMenu:checkPauseState();
                        end
                    end
                end
            end
        elseif ev == "enter" then
            if UIMgr.getCurrentScene().mainMap then
                UIMgr.getCurrentScene().mainMap:stopEffect();
            end

            local function showTop()
                if UIMgr.getCurrentScene().topMenu then
                    UIMgr.getCurrentScene().topMenu:checkPauseState();
                end
            end
            performWithDelay(self, showTop, 0.01);
        end
    end)
end

-- 显示扫荡次数
function UISubAreaList:updateMopupTimes()
    -- 次数
    local scrollview = findChildByName(self.node, "TOP/scrollview");
    local mopUpItem = findChildByName(scrollview, "mopupItem");
    if mopUpItem ~= nil then
        local timesLabel = findChildByName(mopUpItem, "layer");
        local bgBtn = findChildByName(mopUpItem, "btn_bg");
        local areaBg = findChildByName(mopUpItem, "area_bg");
        -- 如果符合扫荡条件
        local ret = MopUpM.canMopUp(self.areaId);
        if true == ret or "life_lack" == ret then
            -- 扫荡功能可用，或仅活力不足
            -- 按钮可点击
            bgBtn:setTouchEnabled(true);
            setGrayMode(bgBtn, false);
            setGrayMode(areaBg, false);
        else
            -- 按钮不可点击
            bgBtn:setTouchEnabled(false);
            setGrayMode(bgBtn, true);
            setGrayMode(areaBg, true);
        end
        local remainTimes = MopUpM.getRemainMopUpTimes(self.areaId);
        local maxTimes = MopUpM.getMaxMopUpTimes(self.areaId);

        TextStyleM.setTextStyle(timesLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, -2, 1.2, nil, nil, nil, true);

        timesLabel:setString(string.format(getLocStr("remainder_times"), remainTimes));
    end
end

-- 尝试扫荡
function UISubAreaList:tryMopUp()
    -- 判断能否扫荡
    local ret = MopUpM.canMopUp(self.areaId);
    if true ~= ret then
        trace("UISubAreaList", "扫荡失败，原因：" .. tostring(ret));

        if ret == "life_lack" then
            -- 失败原因是体力不足，弹出购买体力确认提示
            confirmBuyLife();
        end

        return;
    end

    local requireLife = DungeonAreaM.query(self.areaId, "mop_up_life");

    -- 请求扫荡
    local function sendCmd()
        Operation.cmd_mop_up(self.areaId);
    end
    performWithDelay(self, sendCmd, 1.3);

    local uiForm = UIMopUpEffect.create();
    UIMgr.getCurrentScene():addForm(uiForm);

    playLifeConsumeEffect(requireLife);
end

-- 扫荡成功的回调
function UISubAreaList:whenMopUpDone(args)
    local bonus = args["bonus"];

    -- 重绘扫荡按钮
    self:updateMopupTimes();

    UIMgr.getCurrentScene():removeFormByName("UIMopUp");
    local uiMopUp = UIMopUp.create(args, self.exp, self.level, self.areaId);

    UIMgr.getCurrentScene():removeFormByName("UIMopUpEffect");
    UIMgr.getCurrentScene():addForm(uiMopUp);
end

-- 初始化区域信息
function UISubAreaList:initItem(node, id)
    -- 名称
    local name;
    if DungeonAreaM.isEndlessArea(id) then
        -- 主区域显示“无尽模式”
        name = getLocStr("endless");
    else
        name = DungeonAreaM.query(id, "name");
    end

    -- 关卡背景图片
    local areaBg  = findChildByName(node, "area_bg");
    local bgIndex = DungeonAreaM.query(id, "area_bg");
    areaBg:loadTexture(getSubAreaBgPath(bgIndex));

    -- 难度
    local difficulty = DungeonAreaM.query(id, "layer_fix") + 1;

    -- 进度
    local layerLabel = findChildByName(node, "layer");
    local maxFloor = DungeonAreaM.query(id, "max_floor");

    -- 显示需要解救的冈布奥图标
    local heroImage = findChildByName(node, "hero_icon");
    local slime = DungeonAreaM.getAreaSlime(id);
    if slime ~= nil then
        local heroId = table.values(slime)[1];
        local icon = PetM.getDefaultIcon(heroId);
        if icon ~= nil and not DungeonAreaM.isEndlessArea(id) then
            local heroIconPath = getHeroIconPath(icon);
            heroImage:loadTexture(heroIconPath);
            heroImage:setVisible(true);
        else
            heroImage:setVisible(false);
        end
    end

    -- 设置文本风格
    -- 需要将文字先放大再缩小，以解决描边不平滑的问题
    local nameLabel = findChildByName(node, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_LIGHT_YELLOW, true, -3, 1.2, nil, nil, nil, true);

    TextStyleM.setTextStyle(layerLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, -2, 1.2, nil, nil, nil, true);

    nameLabel:setString(name);

    layerLabel:setString(string.format(getLocStr("sub_area_layer"), maxFloor, difficulty));

    if DungeonAreaM.isEndlessArea(id) then
        -- 主区域显示迷宫事件信息
        local curDp   = AchievementM.calcCurDp(id) or 0;
        local bonusDp = AchievementM.getCurBonusDpValue(id) or 0;
        layerLabel:setString(string.format(getLocStr("dungeon_event_progress"), curDp, bonusDp));
    end

    -- 已通关
    local statusLabel = findChildByName(node, "status");
    if getLang() ~= "zh" and getLang() ~= "tw" then
        statusLabel = findChildByName(node, "status_en");
    end
    TextStyleM.setOutlineStyle(statusLabel, TextStyleM.TEXT_OUTLINE_SIZE, TextStyleM.TEXT_OUTLINE_COLOR_DARK);
    TextStyleM.setTextStyle(statusLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, -2, 1.2);
    statusLabel:setVisible(false);

    local dungeonId = id;
    --注册回调信息
    local bgBtn  = findChildByName(node, "btn_bg");
    local areaBg  = findChildByName(node, "area_bg");
    AddTouchEventListener(bgBtn, function (sender, eventType)
        if eventType == ccui.TouchEventType.began then
            -- 1.变小
            local downTime  = 0.25/2;
            local scaleDown = cc.ScaleTo:create(downTime, 0.95);
            node:runAction(scaleDown);
            -- areaBg:runAction(scaleDown);
        elseif eventType == ccui.TouchEventType.ended then
            -- 2.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1);
            node:runAction(scaleUp);
            -- areaBg:runAction(scaleUp);
        elseif eventType == ccui.TouchEventType.canceled then
            -- 3.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1);
            node:runAction(scaleUp);
            -- areaBg:runAction(scaleUp);
        end

        if eventType == ccui.TouchEventType.ended then
            if DungeonAreaM.isDungeonPassed(ME.user, id) and
                not DungeonAreaM.isDungeonPassBonusTaken(ME.user, id) then
                if DungeonAreaM.canTakeDungeonPassBonus(id) == "life_full" then
                    alert(getLocStr("cannot_take_life_bonus"));
                    return
                end
                Operation.cmd_take_dungeon_pass_bonus(id);
                self:init();
                self:updateMopupTimes();
                UIMgr:getCurrentScene().mainMap:refreshIcons();
                return;
            end

            if DungeonM.isPause() then
                -- 暂离状态(提示状态)
                -- DungeonServiceM.startEnterService(id);
                alert(getLocStr("cant_restart_dungeon"));
                return;
            end

            -- 如果体力不足
            local requireLife = DungeonAreaM.calcDungeonLifeCost(dungeonId);
            if ME.user.dbase:query("life", 0) < requireLife then
                confirmBuyLife();
                return;
            end

            -- 进入地牢
            if UIMgr:getCurrentScene():isOpen("UIEnterDungeon") then
                UIMgr:getCurrentScene():removeForm("UIEnterDungeon");
            end
            local uiEnterDungeon = UIEnterDungeon.create(dungeonId);
            UIMgr.getCurrentScene():addForm(uiEnterDungeon);
            AudioM.playFx("ui_open1");
        end
    end);

    self:updateItem(id);
end

-- 更新外观
function UISubAreaList:updateItem(id)
    local node = self.itemIcons[id];

    -- 已通关信息
    local lifeIcon = findChildByName(node, "life_icon");
    local statusLabel = findChildByName(node, "status");
    if getLang() ~= "zh" and getLang() ~= "tw" then
        statusLabel = findChildByName(node, "status_en");
    end
    local diamondImage = findChildByName(node, "diamond_bg");
    local diamondNumLabel = findChildByName(node, "num");
    local lightImage = findChildByName(node, "light");
    local btnBg = findChildByName(node, "btn_bg");
    local areaBg = findChildByName(node, "area_bg");
    diamondImage:setVisible(false);
    diamondNumLabel:setVisible(false);
    lightImage:setVisible(false);

    if DungeonAreaM.isDungeonPassed(ME.user,id) then
        statusLabel:setVisible(false);
        lifeIcon:setVisible(false);

        if not DungeonAreaM.isDungeonPassBonusTaken(ME.user,id) then
            local rotateForever = cc.RepeatForever:create(cc.RotateBy:create(30, 360));
            lightImage:runAction(rotateForever);
            lightImage:setVisible(true);

            if DungeonAreaM.isLifeBonusArea(id) then
                diamondImage:loadTexture("images/ui/main/bag_of_gem_and_life.png");
            else
                diamondImage:setVisible(true);
            end
            playBubbleAnima2(diamondImage, false, 1);
        else
            statusLabel:setVisible(true);
            statusLabel:setString(getLocStr("sub_area_pass"));
        end
    else
        local lifeCost = DungeonAreaM.calcDungeonLifeCost(id);
        local lifeLayer = createLifeLayer(lifeCost, "right", -0.4);
        statusLabel:setVisible(false);
        lifeIcon:addChild(lifeLayer);
        lifeIcon:setVisible(true);
    end

    if DungeonAreaM.isDungeonPassBonusTaken(ME.user,id) then
        btnBg:setTouchEnabled(false);
        setGrayMode(btnBg, true);
        setGrayMode(areaBg, true);
    else
        btnBg:setTouchEnabled(true);
        setGrayMode(btnBg, false);
        setGrayMode(areaBg, false);
    end
end

-- 获取子区域列表
function UISubAreaList:getSubList()
    local subAreaList = DungeonAreaM.getSubAreaList(self.areaId);

    local finalList = {};
    local mainArea = {};
    for i = 1, #subAreaList do
        -- 判断是否已解锁
        if DungeonAreaM.isSubAreaUnlocked(ME.user, subAreaList[i]) then
            table.insert(finalList, subAreaList[i]);
        end

        if DungeonAreaM.query(subAreaList[i], "parent_id") == 0 then
            table.insert(mainArea, subAreaList[i]);
        end
    end

    -- 如果所有子区域已经通关并且奖励已领取，只显示主区域
    if DungeonAreaM.isAllSubAreaBonusTaken(ME.user, self.areaId) then
        return mainArea;
    end

    return finalList;
end

-- 初始化奖励列表
function UISubAreaList:init(showEffect)
    local scrollview = findChildByName(self.node, "TOP/scrollview");
    scrollview:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/main/SubMapItem.csb");
    local widget = replaceNodeWithWidget(itemNode);

    -- 获取子区域列表
    local subAreaList = self:getSubList();

    -- 左边距
    local leftMargin = 30 + 276;

    -- 行间距
    local gap = 15;
    local widgetHeight = 107;

    local totalNum = #subAreaList;
    if DungeonAreaM.isAllSubAreaPassed(ME.user, self.areaId) and
        -- not DungeonM.isPause() and
        MopUpM.isMopUpActive() then
        totalNum = totalNum + 1;
    end
    self.totalNum = totalNum;

    local startHeight = 108 / 2;

    -- 计算总高度
    local totalHeight = totalNum * (widgetHeight + gap);
    if totalHeight < self.originScrollviewHeight then
        startHeight = self.originScrollviewHeight - totalHeight + 108 / 2;
    end

    scrollview:setInnerContainerSize(cc.size(scrollview:getInnerContainerSize().width,
        math.max(totalHeight, self.originScrollviewHeight)));

    self.itemIcons = {};
    self.items = {};
    local i = 0;

    --local btnMopUp = findChildByName(self.node, "BT/mop_up");
    if DungeonAreaM.isAllSubAreaPassed(ME.user, self.areaId) and
        MopUpM.isMopUpActive() then
        -- 所有小关都已通关
        -- 暂离状态(SLIMEC_3367暂离状态也可以扫荡)
        -- 扫荡功能已经激活
        --btnMopUp:setVisible(true);

        local child = widget:clone();
        local x = leftMargin;
        local y = startHeight + i * (widgetHeight + gap);
        child:setPositionX(x);
        child:setPositionY(y);
        child:setName("mopupItem");

        --self.itemIcons[id] = child;
        table.insert(self.items, child);

        -- 初始化奖励
        child:setTag(i);
        --self:initItem(child, id);
        self:initMopUpItem(child);
        scrollview:addChild(child);
        i = i + 1;
    --else
    --btnMopUp:setVisible(false);
    end

    for _, id in pairs(subAreaList) do
        local child = widget:clone();
        local x = leftMargin;
        local y = startHeight + i * (widgetHeight + gap);
        child:setPositionX(x);
        child:setPositionY(y);
        child:setName("item"..(i + 1));

        self.itemIcons[id] = child;
        table.insert(self.items, child);

        -- 初始化奖励
        child:setTag(i);
        self:initItem(child, id);

        scrollview:addChild(child);
        i = i + 1;
    end

    if showEffect then
        local list = {};
        for j = #self.items, 1, -1 do
            table.insert(list, self.items[j]);
        end
        commonScrollItemFlyIn(list);
    end
end

function UISubAreaList:initMopUpItem(node)
    -- 名称
    local nameLabel = findChildByName(node, "name");

    -- 关卡背景图片
    local areaBg = findChildByName(node, "area_bg");
    local bgIndex = DungeonAreaM.query(self.areaId, "area_bg");
    areaBg:loadTexture(getSubAreaBgPath(bgIndex));

    -- 次数
    local layerLabel = findChildByName(node, "layer");
    local remainTimes = MopUpM.getRemainMopUpTimes(self.areaId);
    local maxTimes = MopUpM.getMaxMopUpTimes(self.areaId);

    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_LIGHT_GREEN, true, -3, 1.2, nil, nil, nil, true);
    TextStyleM.setTextStyle(layerLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, -2, 1.2, nil, nil, nil, true);

    layerLabel:setString(string.format(getLocStr("remainder_times"), remainTimes));
    nameLabel:setString(getLocStr("mopup_text"));

    local lifeIcon = findChildByName(node, "life_icon");
    local statusLabel = findChildByName(node, "status");
    if getLang() ~= "zh" and getLang() ~= "tw" then
        statusLabel = findChildByName(node, "status_en");
    end
    local lifeCost = DungeonAreaM.query(self.areaId, "mop_up_life");
    local lifeLayer = createLifeLayer(lifeCost, "right", -0.4);
    statusLabel:setVisible(false);
    lifeIcon:addChild(lifeLayer);
    lifeIcon:setVisible(true);

    statusLabel:setVisible(false);

    local diamondImage = findChildByName(node, "diamond_bg");
    diamondImage:setVisible(false);
    local diamondNumLabel = findChildByName(node, "num");
    diamondNumLabel:setVisible(false);
    local lightImage = findChildByName(node, "light");
    lightImage:setVisible(false);

    --注册回调信息
    local bgBtn = findChildByName(node, "btn_bg");
    AddTouchEventListener(bgBtn, function (sender, eventType)
        if eventType == ccui.TouchEventType.began then
            -- 1.变小
            local downTime  = 0.25/2;
            local scaleDown = cc.ScaleTo:create(downTime, 0.95);
            node:runAction(scaleDown);
        elseif eventType == ccui.TouchEventType.ended then
            -- 2.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1);
            node:runAction(scaleUp);
        elseif eventType == ccui.TouchEventType.canceled then
            -- 3.变大
            local downTime  = 0.25/2;
            local scaleUp = cc.ScaleTo:create(downTime, 1);
            node:runAction(scaleUp);
        end

        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 获得扫荡前的经验
            self.exp = ME.user:getExp();
            self.level = ME.user:getLevel();

            cclog("扫荡之前的的经验："..ME.user:getExp());
            self:tryMopUp();
        --end
        end
    end);
end

resize = function(self)
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local scale = w / DESIGN_WIDTH;

    -- 设置scroll的新视口高度
    local scrollview = findChildByName(self.node, "TOP/scrollview");
    local scrollViewSize = scrollview:getContentSize();

    local height = (h / scale) - 420;
    scrollview:setContentSize(scrollViewSize.width, height);

    -- 保存窗口适配后的原始高度
    self.originScrollviewHeight = height;

    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");
    AlignM.fitToScreen(node, "bg2");

    -- TOP置于顶部中间并缩放至合适大小
    AlignM.alignToTopCenter(node, "TOP");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 打开排行榜
function UISubAreaList:openRankingList()
    local uiRankingList = UIRankingList.create(self.areaId);
    UIMgr.getCurrentScene():addForm(uiRankingList);
end

-- 重绘底部按钮
function UISubAreaList:redrawButtons()
    -- 重绘迷宫事件按钮提醒
    self:redrawDungeonAchivBubble();

-- 需求变动："迷宫事件"按钮总是显示
--[[
-- 如果主关卡已开启
if DungeonAreaM.isParentAreaUnlocked(ME.user, self.areaId) then
-- 重绘迷宫事件按钮提醒
self:redrawDungeonAchivBubble();
else

-- 还有子关卡未开启时，需要隐藏"迷宫事件"按钮，并重新排布一下按钮的位置
local btnDungeonEvent = findChildByName(self.node, "BT/btn_dungeon");
btnDungeonEvent:setVisible(false);

-- 将'排行榜'按钮移到'迷宫事件'按钮的位置
local btnRankList = findChildByName(self.node, "BT/leaderboard");
btnRankList:setPosition(btnDungeonEvent:getPosition());
end--]]
end

-- 重绘迷宫事件按钮提醒
function UISubAreaList:redrawDungeonAchivBubble()
    local btnDungeonEvent = findChildByName(self.node, "BT/btn_dungeon");
    local hintNode = findChildByName(btnDungeonEvent, "hint");
    btnDungeonEvent:setVisible(true);

    local canGetNum = AchievementM.getAvailableBonusNum(self.areaId);
    checkBlueBubbleStatus(hintNode, canGetNum);
end

-- 显示昵称国家输入界面
function UISubAreaList:showRankNickInput()
    local function callback()
        closeFormByName("UIRankNick")
        local uiForm = UIRankNick.create(self.areaId);
        UIMgr.getCurrentScene():addForm(uiForm);
    end
    checkIfGuestNeedBind(callback);
end


